/*
 * Created on Feb 21, 2005
 */
package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.sales.forms.SendAckMailForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.commons.Location;
import com.scalar.chiptrack.operations.vendor.Vendor;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;


/**
 * @author Murali Krishna
**/
public class OpenPurchaseOrderShowSendMailCommand extends ActionCommand
{
	/*
	 * Used to display the window using which, purchase order acknowledgements can be sent
	 */
	public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HashMap errors			= ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info            = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String actionForward 	= "showSendMailWindow";
		String action   		= request.getParameter( REQUEST_ACTION );
        String purchaseOrderId  = request.getParameter( "purchaseOrderId");
        String vendorId         = request.getParameter( "vendorId");
        String poNumber         = request.getParameter( "poNumber");
        String poTypeAsString   = request.getParameter( "poType" ) ;
        HttpSession mSession 	= request.getSession();
        int    poType           = Integer.parseInt( poTypeAsString );
        String subject          = null;

        request.setAttribute ( "purchaseOrderId", purchaseOrderId );
        request.setAttribute ( "vendorId", vendorId );

        try
        {
            if( action.equalsIgnoreCase( "showSendMailWindow" ) )
            {
                SendAckMailForm sendAckMailForm = ( SendAckMailForm ) form;
                User user = (User) mSession.getAttribute( USER_KEY);
                String[] toUsers = new String[2];
                String mimeType = sendAckMailForm.getAttachMIMEType ();
                if ( ( mimeType == null ) || ( mimeType.trim ().length () == 0 ) )
                {
                    mimeType = "PDF";
                    sendAckMailForm.setAttachMIMEType ( "PDF" );
                }

                if ( subject == null )
                {
                    if( poType == 1 )
                    {
                        subject = "Open Purchase Order Acknowledgement for "+poNumber;
                    }
                    else if( poType == 2 )
                    {
                        subject = "Standard Purchase Order Acknowledgement for "+poNumber;
                    }
                    else if( poType == 3 )
                    {
                        subject = "DropShip Purchase Order Acknowledgement for "+poNumber;
                    }
                    else
                    {
                        String receiptId         = request.getParameter( "receiptId");
                        request.setAttribute ( "receiptId", receiptId );
                        subject = "Purchase Order Receipt Acknowledgement for "+poNumber;
                    }

                }
                sendAckMailForm.setSubject ( subject );

                Vendor vendor = VendorDAO.getVendorDetails( Integer.parseInt(vendorId) );

                /* currently loading the emails of vendor and the current user's emails only*/
                String vendorEmail =((Location)vendor.getLocationsList().get( 0 )).getLocationAddress().getEmail();
                toUsers[0] = user.getEmail();
                toUsers[1] =  vendorEmail;

                String from = user.getEmail ();

                /* cc, bcc are set to null as of now*/
                sendAckMailForm.setCcUsers( null);
                sendAckMailForm.setBccUsers( null);

                sendAckMailForm.setFromUser( from );
                sendAckMailForm.setToUsers(toUsers);
                request.setAttribute ( "sendAckMailForm", sendAckMailForm );
                actionForward = "showSendMailWindow";
            }
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            Logger.error( ERROR_LOADING_OPEN_PURCHASE_ORDER_INFORMATION, e );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_OPEN_PURCHASE_ORDER_INFORMATION );
            mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
            actionForward = "saveSuccess";
        }

        request.setAttribute( ERROR_MESSAGES, errors );
        request.setAttribute( INFO_MESSAGES, info );
        return mapping.findForward( actionForward );
    }
}